package com.gouwudai.gobbs.dao.impl;

import org.apache.log4j.Logger;

import com.gouwudai.gobbs.common.IPageList;
import com.gouwudai.gobbs.dao.IMasterGradePermissionDAO;
import com.gouwudai.gobbs.pojo.TbbsMasterGradePermission;
import com.litt.core.common.Utility;
import com.litt.core.exception.DBException;

public class MasterGradePermissionDAOImpl extends BaseDAOImpl implements IMasterGradePermissionDAO 
{
	private static final Logger logger = Logger.getLogger(MasterGradePermissionDAOImpl.class);	
	private static final String BUSI_NAME = "管理员等级权限信息";
	
	public void save(TbbsMasterGradePermission tbbsMasterGradePermission)
	{
		super.saveObject(tbbsMasterGradePermission);		
	}
    
   
    public void delete(Integer mgpId)
    {
        super.deleteObject(TbbsMasterGradePermission.class.getName(),"mgpId",mgpId);
    }
    
    public void saveOrUpdate(Integer mgId,Integer permissionCode,boolean flag)
    {
    	if(flag)	//增加
    	{
    		TbbsMasterGradePermission tbbsMasterGradePermission = new TbbsMasterGradePermission();
    		tbbsMasterGradePermission.setMgId(mgId);
    		tbbsMasterGradePermission.setPermissionCode(permissionCode);
    		super.saveObject(tbbsMasterGradePermission);
    	}	
    	else	//删除
    	{
    		String hql = "delete from TbbsMasterGradePermission where mgId="+mgId+" and permissionCode="+permissionCode;
    		super.executeHql(hql);
    	}
    }
    
    /**
     * 
     * @param cond   查询条件：1-MG_ID，2-PERMISSION_CODE，3-PERMISSION_NAME，4-QUERY_FLAG(0-全部，1-已拥有权限，2-未拥有权限)
     * @param pageIndex
     * @param pageSize
     * @param sort
     * @return
     * @throws DBException
     */
    public IPageList listPage(String[] cond,int pageIndex,int pageSize,String sort)
    {       
    	int flag = Utility.parseInt(cond[3]);
    	
    	StringBuilder listSql = new StringBuilder("SELECT 'CHECKED' AS FLAG, * FROM TbbsMasterPermission WHERE PERMISSION_CODE IN (SELECT A.PERMISSION_CODE FROM TbbsMasterGradePermission A,TbbsMasterGrade B WHERE B.MG_ID="+cond[0]+" AND A.MG_ID=B.MG_ID )");
       
    	StringBuilder listSql2 = new StringBuilder(" SELECT '' AS FLAG, * FROM TbbsMasterPermission WHERE PERMISSION_CODE NOT IN (SELECT A.PERMISSION_CODE FROM TbbsMasterGradePermission A,TbbsMasterGrade B WHERE B.MG_ID="+cond[0]+" AND A.MG_ID=B.MG_ID)");
	
       if(cond[1]!=null&&!cond[1].equals(""))
       {
       		listSql.append(" AND A.PERMISSION_CODE=");
       		listSql.append(cond[1]); 
       		
       		listSql2.append(" AND A.PERMISSION_CODE=");
       		listSql2.append(cond[1]);  
       }
       if(cond[2]!=null&&!cond[2].equals(""))
       {
    	    listSql.append(" AND B.PERMISSION_NAME='");
        	listSql.append(cond[2]);
        	listSql.append("'"); 
        	
        	listSql2.append(" AND B.PERMISSION_NAME='");
        	listSql2.append(cond[2]);
        	listSql2.append("'"); 
       } 
       switch(flag)
       {
	       case 1:
	    	   	break;
	       case 2:
	    	   listSql = listSql2;
	    	   	break;
	       default :
	    	   listSql.append(" UNION ALL");
	    	   listSql.append(listSql2);
       
       }
       
       
       
       //没有点表格排序时的默认排序
       if(sort.indexOf("ORDER")<0)  
    	   listSql.append(" ORDER BY PERMISSION_CODE ASC");
       else
    	   listSql.append(sort); 
    	return super.listSqlPage(listSql.toString(),pageIndex,pageSize);
    }
    
}
